****************************************************************************************
*** Replication Code                                                                 ***
*** Title: Overestimation of the Level of Democracy among Citizens in Nondemocracies ***
*** Author: Eddy S. F. Yeung                                                         ***
*** Version: March 5, 2022                                                           ***
****************************************************************************************

*** Set-up ***
// Clean the Stata environment and set the working directory
// Stata version BE 17.0 for Mac (Intel 64-bit)
clear all
cd "~/Desktop/CPS_replication"
use "dataset_cleaned.dta"

*** Recode variables ***
// Reverse-code Freedom of the Press (0 = least free; 100 = most free)
gen new_fotp = 100 - fotp

// Rescale Political Terror Scale (0 = least political terror; 100 = most political terror)
gen new_pts = (100/4) * (PTS - 5) + 100 // transform from 1 to 5 to 0 to 100

// Rescale Media System Freedom (0 = least free; 100 = most free)
gen new_msf = MSF * 100

// Rescale Rule of Law (0 = weakest; 100 = strongest)
gen new_rol = (100/5) * (ROL - 2.5) + 100 // transform from -2.5 to 2.5 to 0 to 100

// Rescale Government Effectiveness (0 = weakest; 100 = strongest)
gen new_gov = (100/5) * (GOV - 2.5) + 100 // transform from -2.5 to 2.5 to 0 to 100

// Marital status (V57)
gen married = .
replace married = 1 if V57 == 1
replace married = 0 if V57 > 1 & V57 < 7

// Perceived level of democracy (V141)
gen per_dem = V141
egen per_dem_z = std(per_dem) // standardized version of per_dem

// Employment status (V229)
gen unemployed = .
replace unemployed = 1 if V229 == 7
replace unemployed = 0 if (V229 > 0 & V229 < 7) | V229 == 8

// Subjective social class (V238)
gen social_class = .
replace social_class = 0 if V238 == 5
replace social_class = 1 if V238 == 4
replace social_class = 2 if V238 == 3
replace social_class = 3 if V238 == 2
replace social_class = 4 if V238 == 1

// Income (V239)
gen income = .
replace income = 0 if V239 == 1
replace income = 1 if V239 == 2
replace income = 2 if V239 == 3
replace income = 3 if V239 == 4
replace income = 4 if V239 == 5
replace income = 5 if V239 == 6
replace income = 6 if V239 == 7
replace income = 7 if V239 == 8
replace income = 8 if V239 == 9
replace income = 9 if V239 == 10

// Sex (V240)
gen female = .
replace female = 1 if V240 == 2
replace female = 0 if V240 == 1

// Age (V242)
gen age = V242
replace age = . if V242 == -1 | V242 == -2 | V242 == -3 | V242 == -4 | V242 == -5

// Education (V248)
gen university = .
replace university = 1 if V248 == 9
replace university = 0 if V248 > 0 & V248 < 9

// Media consumption (V217, V219, V220, V223)
gen newspaper = .
replace newspaper = 0 if V217 == 5
replace newspaper = 1 if V217 == 4
replace newspaper = 2 if V217 == 3
replace newspaper = 3 if V217 == 2
replace newspaper = 4 if V217 == 1

gen tv = .
replace tv = 0 if V219 == 5
replace tv = 1 if V219 == 4
replace tv = 2 if V219 == 3
replace tv = 3 if V219 == 2
replace tv = 4 if V219 == 1

gen radio = .
replace radio = 0 if V220 == 5
replace radio = 1 if V220 == 4
replace radio = 2 if V220 == 3
replace radio = 3 if V220 == 2
replace radio = 4 if V220 == 1

gen internet = .
replace internet = 0 if V223 == 5
replace internet = 1 if V223 == 4
replace internet = 2 if V223 == 3
replace internet = 3 if V223 == 2
replace internet = 4 if V223 == 1

// Confidence in the courts (V114)
gen conf_courts = .
replace conf_courts = 0 if V114 == 4
replace conf_courts = 1 if V114 == 3
replace conf_courts = 2 if V114 == 2
replace conf_courts = 3 if V114 == 1

// Confidence in the government (V115)
gen conf_govt = .
replace conf_govt = 0 if V115 == 4
replace conf_govt = 1 if V115 == 3
replace conf_govt = 2 if V115 == 2
replace conf_govt = 3 if V115 == 1

// Generate age squared variable
gen age_sq = age ^ 2 / 100

// Generate GDP-related variables
gen ln_gdp = ln(gdp)
replace growth_one_yr = growth_one_yr * 100
replace growth_two_yr_avg = growth_two_yr_avg * 100
replace growth_five_yr = growth_five_yr * 100

*** Additional data processing -- part 1 ***
// Drop unclassified regimes and democracies (based on v2x_regime)
drop if v2x_regime == .
drop if v2x_regime == 2 | v2x_regime == 3

// Drop regimes that do not have both media freedom variables available
drop if new_fotp == . | new_msf == .

*** Table S6 ***
// Check if media freedom predicts nonresponses in nondemocracies
// Generate an indicator for nonresponses
gen nonresponse = .
replace nonresponse = 1 if V141 < 0
replace nonresponse = 0 if V141 >= 1 & V141 <= 10

// Model 1
eststo NA1: mixed nonresponse new_fotp university ///
	female age age_sq married unemployed income social_class || ///
	code: university, vce(cluster code) mle
estat ic

// Model 2
eststo NA2: mixed nonresponse new_msf university ///
	female age age_sq married unemployed income social_class || ///
	code: university, vce(cluster code) mle
estat ic

// Export table (Table S6 is modified from this exported table)
esttab NA* using "~/Desktop/CPS_replication/tables/Table S6.tex", ///
	replace se b(4) star(* 0.10 ** 0.05 *** 0.01)
eststo clear

*** Additional data processing -- part 2 ***
// Drop non-respondents of DV (V141)
drop if V141 == -1 | V141 == -2 | V141 == -3 | V141 == -4 | V141 == -5

// Rescale democracy scores of V-Dem (v2x_polyarchy)
gen tran_vdem = (9/1) * (v2x_polyarchy - 1) + 10 // transform from 0 to 1 to 1 to 10
gen rescaled_vdem = .
replace rescaled_vdem =  1 if tran_vdem >= 0.5 & tran_vdem < 1.5
replace rescaled_vdem =  2 if tran_vdem >= 1.5 & tran_vdem < 2.5
replace rescaled_vdem =  3 if tran_vdem >= 2.5 & tran_vdem < 3.5
replace rescaled_vdem =  4 if tran_vdem >= 3.5 & tran_vdem < 4.5
replace rescaled_vdem =  5 if tran_vdem >= 4.5 & tran_vdem < 5.5
replace rescaled_vdem =  6 if tran_vdem >= 5.5 & tran_vdem < 6.5
replace rescaled_vdem =  7 if tran_vdem >= 6.5 & tran_vdem < 7.5
replace rescaled_vdem =  8 if tran_vdem >= 7.5 & tran_vdem < 8.5
replace rescaled_vdem =  9 if tran_vdem >= 8.5 & tran_vdem < 9.5
replace rescaled_vdem = 10 if tran_vdem >= 9.5 & tran_vdem < 10.5

// Discrepancy between perceived and measured levels of democracy (based on V-Dem)
gen diff_dem_vdem = per_dem - rescaled_vdem
gen diff_dem_vdem_z = per_dem_z - v2x_polyarchy_z

// Dummy variable indicating whether overestimation took place (based on V-Dem)
gen overest_vdem = .
replace overest_vdem = 1 if (per_dem >  rescaled_vdem) & (v2x_polyarchy != .)
replace overest_vdem = 0 if (per_dem <= rescaled_vdem) & (v2x_polyarchy != .)

// Rescale democracy scores of Polity5
gen tran_polity = (9/20) * (polity2 - 10) + 10	// transform from -10 to 10 to 1 to 10
gen rescaled_polity = .
replace rescaled_polity =  1 if tran_polity >= 0.5 & tran_polity < 1.5
replace rescaled_polity =  2 if tran_polity >= 1.5 & tran_polity < 2.5
replace rescaled_polity =  3 if tran_polity >= 2.5 & tran_polity < 3.5
replace rescaled_polity =  4 if tran_polity >= 3.5 & tran_polity < 4.5
replace rescaled_polity =  5 if tran_polity >= 4.5 & tran_polity < 5.5
replace rescaled_polity =  6 if tran_polity >= 5.5 & tran_polity < 6.5
replace rescaled_polity =  7 if tran_polity >= 6.5 & tran_polity < 7.5
replace rescaled_polity =  8 if tran_polity >= 7.5 & tran_polity < 8.5
replace rescaled_polity =  9 if tran_polity >= 8.5 & tran_polity < 9.5
replace rescaled_polity = 10 if tran_polity >= 9.5 & tran_polity < 10.5

// Discrepancy between perceived and measured levels of democracy (based on Polity5)
gen diff_dem_polity = per_dem - rescaled_polity
	
// Dummy variable indicating whether overestimation took place (based on Polity5)
gen overest_polity = .
replace overest_polity = 1 if (per_dem >  rescaled_polity) & (polity2 != .)
replace overest_polity = 0 if (per_dem <= rescaled_polity) & (polity2 != .)

// Rescale democracy scores of Freedom in the World
gen new_fitw = 8 - fitw	// reverse-code fitw
gen tran_fitw = (9/6) * (new_fitw - 7) + 10	// transform from 1 to 7 to 1 to 10
gen rescaled_fitw = .
replace rescaled_fitw =  1 if tran_fitw >= 0.5 & tran_fitw < 1.5
replace rescaled_fitw =  2 if tran_fitw >= 1.5 & tran_fitw < 2.5
replace rescaled_fitw =  3 if tran_fitw >= 2.5 & tran_fitw < 3.5
replace rescaled_fitw =  4 if tran_fitw >= 3.5 & tran_fitw < 4.5
replace rescaled_fitw =  5 if tran_fitw >= 4.5 & tran_fitw < 5.5
replace rescaled_fitw =  6 if tran_fitw >= 5.5 & tran_fitw < 6.5
replace rescaled_fitw =  7 if tran_fitw >= 6.5 & tran_fitw < 7.5
replace rescaled_fitw =  8 if tran_fitw >= 7.5 & tran_fitw < 8.5
replace rescaled_fitw =  9 if tran_fitw >= 8.5 & tran_fitw < 9.5
replace rescaled_fitw = 10 if tran_fitw >= 9.5 & tran_fitw < 10.5

// Discrepancy between perceived and measured levels of democracy (based on Freedom in the World)
gen diff_dem_fitw = per_dem - rescaled_fitw

// Dummy variable indicating whether overestimation took place (based on Freedom in the World)
gen overest_fitw = .
replace overest_fitw = 1 if (per_dem >  rescaled_fitw) & (fitw != .)
replace overest_fitw = 0 if (per_dem <= rescaled_fitw) & (fitw != .)

// Rescale liberal democracy index (v2x_libdem)
gen tran_libdem = (9/1) * (v2x_libdem - 1) + 10
gen rescaled_libdem = .
replace rescaled_libdem =  1 if tran_libdem >= 0.5 & tran_libdem < 1.5
replace rescaled_libdem =  2 if tran_libdem >= 1.5 & tran_libdem < 2.5
replace rescaled_libdem =  3 if tran_libdem >= 2.5 & tran_libdem < 3.5
replace rescaled_libdem =  4 if tran_libdem >= 3.5 & tran_libdem < 4.5
replace rescaled_libdem =  5 if tran_libdem >= 4.5 & tran_libdem < 5.5
replace rescaled_libdem =  6 if tran_libdem >= 5.5 & tran_libdem < 6.5
replace rescaled_libdem =  7 if tran_libdem >= 6.5 & tran_libdem < 7.5
replace rescaled_libdem =  8 if tran_libdem >= 7.5 & tran_libdem < 8.5
replace rescaled_libdem =  9 if tran_libdem >= 8.5 & tran_libdem < 9.5
replace rescaled_libdem = 10 if tran_libdem >= 9.5 & tran_libdem < 10.5

// Discrepancy between perceived and measured levels of democracy (based on v2x_libdem)
gen diff_dem_libdem = per_dem - rescaled_libdem

// Dummy variable indicating whether overestimation took place (based on v2x_libdem)
gen overest_libdem = .
replace overest_libdem = 1 if (per_dem >  rescaled_libdem) & (v2x_libdem != .)
replace overest_libdem = 0 if (per_dem <= rescaled_libdem) & (v2x_libdem != .)

// Rescale participatory democracy index (v2x_partipdem)
gen tran_partipdem = (9/1) * (v2x_partipdem - 1) + 10
gen rescaled_partipdem = .
replace rescaled_partipdem =  1 if tran_partipdem >= 0.5 & tran_partipdem < 1.5
replace rescaled_partipdem =  2 if tran_partipdem >= 1.5 & tran_partipdem < 2.5
replace rescaled_partipdem =  3 if tran_partipdem >= 2.5 & tran_partipdem < 3.5
replace rescaled_partipdem =  4 if tran_partipdem >= 3.5 & tran_partipdem < 4.5
replace rescaled_partipdem =  5 if tran_partipdem >= 4.5 & tran_partipdem < 5.5
replace rescaled_partipdem =  6 if tran_partipdem >= 5.5 & tran_partipdem < 6.5
replace rescaled_partipdem =  7 if tran_partipdem >= 6.5 & tran_partipdem < 7.5
replace rescaled_partipdem =  8 if tran_partipdem >= 7.5 & tran_partipdem < 8.5
replace rescaled_partipdem =  9 if tran_partipdem >= 8.5 & tran_partipdem < 9.5
replace rescaled_partipdem = 10 if tran_partipdem >= 9.5 & tran_partipdem < 10.5

// Discrepancy between perceived and measured levels of democracy (based on v2x_partipdem)
gen diff_dem_partipdem = per_dem - rescaled_partipdem

// Dummy variable indicating whether overestimation took place (based on v2x_partipdem)
gen overest_partipdem = .
replace overest_partipdem = 1 if (per_dem >  rescaled_partipdem) & (v2x_partipdem != .)
replace overest_partipdem = 0 if (per_dem <= rescaled_partipdem) & (v2x_partipdem != .)

// Rescale deliberative democracy index (v2x_delibdem)
gen tran_delibdem = (9/1) * (v2x_delibdem - 1) + 10
gen rescaled_delibdem = .
replace rescaled_delibdem =  1 if tran_delibdem >= 0.5 & tran_delibdem < 1.5
replace rescaled_delibdem =  2 if tran_delibdem >= 1.5 & tran_delibdem < 2.5
replace rescaled_delibdem =  3 if tran_delibdem >= 2.5 & tran_delibdem < 3.5
replace rescaled_delibdem =  4 if tran_delibdem >= 3.5 & tran_delibdem < 4.5
replace rescaled_delibdem =  5 if tran_delibdem >= 4.5 & tran_delibdem < 5.5
replace rescaled_delibdem =  6 if tran_delibdem >= 5.5 & tran_delibdem < 6.5
replace rescaled_delibdem =  7 if tran_delibdem >= 6.5 & tran_delibdem < 7.5
replace rescaled_delibdem =  8 if tran_delibdem >= 7.5 & tran_delibdem < 8.5
replace rescaled_delibdem =  9 if tran_delibdem >= 8.5 & tran_delibdem < 9.5
replace rescaled_delibdem = 10 if tran_delibdem >= 9.5 & tran_delibdem < 10.5

// Discrepancy between perceived and measured levels of democracy (based on v2x_delibdem)
gen diff_dem_delibdem = per_dem - rescaled_delibdem

// Dummy variable indicating whether overestimation took place (based on v2x_delibdem)
gen overest_delibdem = .
replace overest_delibdem = 1 if (per_dem >  rescaled_delibdem) & (v2x_delibdem != .)
replace overest_delibdem = 0 if (per_dem <= rescaled_delibdem) & (v2x_delibdem != .)

// Rescale egalitarian democracy index (v2x_egaldem)
gen tran_egaldem = (9/1) * (v2x_egaldem - 1) + 10
gen rescaled_egaldem = .
replace rescaled_egaldem =  1 if tran_egaldem >= 0.5 & tran_egaldem < 1.5
replace rescaled_egaldem =  2 if tran_egaldem >= 1.5 & tran_egaldem < 2.5
replace rescaled_egaldem =  3 if tran_egaldem >= 2.5 & tran_egaldem < 3.5
replace rescaled_egaldem =  4 if tran_egaldem >= 3.5 & tran_egaldem < 4.5
replace rescaled_egaldem =  5 if tran_egaldem >= 4.5 & tran_egaldem < 5.5
replace rescaled_egaldem =  6 if tran_egaldem >= 5.5 & tran_egaldem < 6.5
replace rescaled_egaldem =  7 if tran_egaldem >= 6.5 & tran_egaldem < 7.5
replace rescaled_egaldem =  8 if tran_egaldem >= 7.5 & tran_egaldem < 8.5
replace rescaled_egaldem =  9 if tran_egaldem >= 8.5 & tran_egaldem < 9.5
replace rescaled_egaldem = 10 if tran_egaldem >= 9.5 & tran_egaldem < 10.5

// Discrepancy between perceived and measured levels of democracy (based on v2x_egaldem)
gen diff_dem_egaldem = per_dem - rescaled_egaldem

// Dummy variable indicating whether overestimation took place (based on v2x_egaldem)
gen overest_egaldem = .
replace overest_egaldem = 1 if (per_dem >  rescaled_egaldem) & (v2x_egaldem != .)
replace overest_egaldem = 0 if (per_dem <= rescaled_egaldem) & (v2x_egaldem != .)

// Construct a new v2x_polyarchy index that drops v2x_freexp_altinf
gen new_polyarchy = .5 * (v2x_elecoff * v2xel_frefair * v2x_frassoc_thick * v2x_suffr) ///
	+ .5 * (1/6 * v2x_elecoff + 1/3 * v2xel_frefair + 1/3 * v2x_frassoc_thick ///
	+ 1/6 * v2x_suffr)
	
// Rescale the new v2x_polyarchy index
gen tran_new_vdem = (9/1) * (new_polyarchy - 1) + 10 // transform from 0 to 1 to 1 to 10
gen rescaled_new_vdem = .
replace rescaled_new_vdem =  1 if tran_new_vdem >= 0.5 & tran_new_vdem < 1.5
replace rescaled_new_vdem =  2 if tran_new_vdem >= 1.5 & tran_new_vdem < 2.5
replace rescaled_new_vdem =  3 if tran_new_vdem >= 2.5 & tran_new_vdem < 3.5
replace rescaled_new_vdem =  4 if tran_new_vdem >= 3.5 & tran_new_vdem < 4.5
replace rescaled_new_vdem =  5 if tran_new_vdem >= 4.5 & tran_new_vdem < 5.5
replace rescaled_new_vdem =  6 if tran_new_vdem >= 5.5 & tran_new_vdem < 6.5
replace rescaled_new_vdem =  7 if tran_new_vdem >= 6.5 & tran_new_vdem < 7.5
replace rescaled_new_vdem =  8 if tran_new_vdem >= 7.5 & tran_new_vdem < 8.5
replace rescaled_new_vdem =  9 if tran_new_vdem >= 8.5 & tran_new_vdem < 9.5
replace rescaled_new_vdem = 10 if tran_new_vdem >= 9.5 & tran_new_vdem < 10.5

// Discrepancy between perceived and measured levels of democracy (based on new v2x_polyarchy index)
gen diff_dem_new_vdem = per_dem - rescaled_new_vdem
	
*** Export the dataset ***
// Export the main, final dataset as "dataset_final.dta"
save "~/Desktop/CPS_replication/dataset_final.dta", replace

*** Figure 3 ***
// Set the plot scheme (run the two following lines to replicate the asethetics of the graphs)
* net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
* set scheme cleanplots

// Histogram for diff_dem_vdem
histogram diff_dem_vdem, discrete freq xtitle(Degree of Overestimation) ///
	xscale(range(-6 8)) ysize(3.5) xsize(6) xlabel(-6 -3 0 3 6)
graph export "~/Desktop/CPS_replication/figures/Figure 3.pdf", replace

*** Figure S14 ***
// Histogram for diff_dem_vdem_z
histogram diff_dem_vdem_z, freq bin(15) xtitle(Degree of Overestimation) ///
	xscale(range(-2 3.5)) ysize(3.5) xsize(6) xlabel(-2 -1 0 1 2 3)
graph export "~/Desktop/CPS_replication/figures/Figure S14.pdf", replace

*** Table 1 ***
// Summary statistics for individual-level variables
estpost sum diff_dem_vdem university ///
	female age age_sq married unemployed income social_class

// Export table (Table 1 is modified from this exported table)
esttab using "~/Desktop/CPS_replication/tables/Table 1a.tex", ///
	cells("count mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(3))") replace
eststo clear

// Summary statistics for country-level variables
preserve
collapse (mean) new_fotp new_msf ln_gdp growth_one_yr new_rol new_gov, by(code)
estpost sum new_fotp new_msf ln_gdp growth_one_yr new_rol new_gov

// Export table (Table 1 is modified from this exported table)
esttab using "~/Desktop/CPS_replication/tables/Table 1b.tex", ///
	cells("count mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(3))") replace
eststo clear
restore
